package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.ChannelMapper;
import com.heima.admin.service.ChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, AdChannel> implements ChannelService {

    @Override
    public PageResponseResult findByNameAndPage(ChannelDTO dto) {
        // 1. 校验参数
        //校验分页参数是否违规
        dto.checkParam();
        //封装分页条件
        // 2. 封装条件 执行查询
        // 2.1  封装分页查询条件
        Page<AdChannel> adChannelPage = new Page<>(dto.getPage(), dto.getSize());
        // 2.2 其他查询条件
        LambdaQueryWrapper<AdChannel> query = Wrappers.lambdaQuery();
        if (Strings.isNotEmpty(dto.getName())) {
            query.like(AdChannel::getName, dto.getName());
        }
        if (dto.getStatus() != null) {
            query.eq(AdChannel::getStatus, dto.getStatus());
        }
        //按照ord进行升序排序
        query.orderByAsc(AdChannel::getOrd);
        // 2.3 执行查询操作
        IPage<AdChannel> pageResult = page(adChannelPage, query);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());
        //3. 封装成PageResponseResult结果返回
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.频道名称不能为空
        if (Strings.isBlank(adChannel.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "频道名称不能为空");
        }
        //2.频道长度不能大于10
        if (adChannel.getName().length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道长度不能大于10");
        }
        //频道名称不能重复
        int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道已经存在，无法添加");
        }
        adChannel.setCreatedTime(new Date());
        this.save(adChannel);
        //封装返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateChannel(AdChannel adChannel) {
        //1 参数校验
        if (adChannel.getId()==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道id不能为空");
        }
        //2 执行修改
        //获取原来的频道
        AdChannel  channel = getById(adChannel.getId());
        if (channel==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
        }
        //判断参数name是否为空，如果频道名称不为空判断和原来的频道名称是否一致
        String name = adChannel.getName();
        if (Strings.isNotBlank(name)||name.equals(channel.getName())) {
            //2.频道长度不能大于10
            if (adChannel.getName().length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "频道长度不能大于10");
            }
            //频道名称不能重复
            int count = this.count(Wrappers.<AdChannel>lambdaQuery().eq(AdChannel::getName, adChannel.getName()));
            if (count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该频道已经存在，无法添加");
            }
        }
        //修改频道
        this.updateById(adChannel);
        //4 返回结果
        return ResponseResult.okResult();
    }
    @Transactional
    @Override
    public ResponseResult deleteChannel(Integer id) {
        //1.校验参数
        if (id==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道id不能为空");
        }
        //根据id查询频道
        AdChannel oldChannel = getById(id);
        //判断频道是否启用
        if (oldChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"该频道不存在，无法进行删除");
        }
        if (oldChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"该频道正在被启用，无法删除");
        }
        //2.如果未启用 删除频道
        this.removeById(id);
        //3.返回响应结果
        return ResponseResult.okResult();
    }
}
